package com.tencent.albert.spring.shiro;

import com.tencent.albert.spring.utils.Cryptos;
import com.tencent.albert.spring.utils.Encodes;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by albertzhu on 2015/2/18.
 */
public class SsoOpenIdCryptos {

    public static final String cookie_name = "sso_openId";

    private static Logger logger = LoggerFactory.getLogger(SsoOpenIdCryptos.class);

    private final static byte[] hexCookieKey = Encodes.decodeHex("7f4e47dd0f1b6b2594ba38c3817a2525");

    public static String encryptOpenId(String openId) {
        String value = Encodes.encodeHex(Cryptos.aesEncrypt(openId.getBytes(), hexCookieKey));
        return value;
    }

    public static String decryptOpenId(String value) {
        if (!StringUtils.isBlank(value)) {
            try {
                byte[] temps = Encodes.decodeHex(value);
                String openId = new String(Cryptos.aesDecrypt(temps, hexCookieKey));
                return openId;
            } catch (Exception e) {
                logger.info("decryptOpenId openId error "+value+" e:"+e,e);
            }
        }
        return null;
    }

}

